﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Refugees.Repositories
{
    internal class UserRepository : ModelRepository
    {
        internal Models.User Login(string username, string password)
        {
            return Context.User.ToList().FirstOrDefault(i => i.Email == username && i.Password == password);
        }


        internal List<Models.PersonalData> Personals()
        {
            return User.PersonalData.ToList();
        }

        internal List<Models.User> Users()
        {
            return Context.User.ToList();
        }

        internal List<Models.Role> Roles ()
        {
            return Context.Role.ToList();
        }

        internal Models.User Registration(string username, string password, string phone)
        {
            var user = new Models.User()
            {
                Email = username,
                Password = password,
                Phone = phone,
                role_id=1,
            };
            try
            {
                Context.User.Add(user);
                Context.SaveChanges();
                return Context.User.FirstOrDefault(i => i.Email == username && i.Password == password);

            }
            catch (Exception)
            {
                Context.User.Remove(user);
            }

            return new Models.User();
        }

        internal bool Exist(string username)
        {
            return Context.User.Where(i => i.Email == username).Count()>0;
        }
    }
}
